package org.jianfa.learn.dubbo.filter;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

public class DataCollection {

    private static final ConcurrentMap<String, DataDistribution> DATA_DISTRIBUTION_CONCURRENT_MAP = new ConcurrentHashMap();

    public static DataDistribution getDataDistribution(String methodName) {
        DataDistribution dataDistribution = DATA_DISTRIBUTION_CONCURRENT_MAP.get(methodName);
        if (dataDistribution == null) {
            DATA_DISTRIBUTION_CONCURRENT_MAP.putIfAbsent(methodName, new DataDistribution(methodName));
            dataDistribution = DATA_DISTRIBUTION_CONCURRENT_MAP.get(methodName);
        }
        return dataDistribution;
    }

    public static final void recordResponseTime(String methodName, double responseTime) {
        DataDistribution dataDistribution = getDataDistribution(methodName);
        dataDistribution.recoredResponseTime(responseTime);
    }

    public static final void reportResponseTime() {
        System.out.printf("%n");
        for (Map.Entry<String, DataDistribution> DATA_DISTRIBUTION_CONCURRENT_MAP_ENTRY : DATA_DISTRIBUTION_CONCURRENT_MAP.entrySet()) {
            DataDistribution dataDistribution = DATA_DISTRIBUTION_CONCURRENT_MAP_ENTRY.getValue();
            dataDistribution.report();
        }
        System.out.printf("%n");
    }
}
